/*
 * Script: moossage.js
 *  My Fading message log
 * 
 * Author:
 * 	Jakub Miara (jakub.miara@gmail.com)
 * 
 * License:
 * 	MIT-style license.
 * 
 * Requires mootools v 1.1
 * version v0.2
 */
 
var Moossage = new Class({
	
		getOptions: function(){
			return {
				delayTime: 6000,
				appearTime: 500,
				autoClose: true
			};
	  },
	  	
	  divs: Array(),
	  version: "moossage v0.2",
	  
		initialize: function(options){
			this.setOptions(this.getOptions(), options);
		},

		generateCloseButton: function(){
			var closeButton = document.createElement("div");

			if ($defined(this.options.closeButtonClass))
				$(closeButton).addClass(this.options.closeButtonClass);
			else
				this.setCloseButtonStyles(closeButton);
				
			$(closeButton).setHTML("x");
			$(closeButton).onclick = this.close.bindWithEvent(this);
			return closeButton;
		},
		
		setMessageStyles: function(messageDiv){
			$(messageDiv).setStyles({
				width: "400px",
				border: "1px solid",
				textAlign: "center",
				position: "fixed",
				bottom: "0",
				backgroundColor: "black",
				color: "white",
				padding: "20px"
			 });
			
		},
		
		setCloseButtonStyles: function(closeButtonDiv){
			$(closeButtonDiv).setStyles({
				fontSize : "10px",
				backgroundColor: "orange",
				position: "absolute",
				right: "0px",
				top: "0px",
				cursor: "pointer",
				padding: "2px",
				margin: "0px",
				color: "black"
			});
		},
		
		addMessage: function(txt, backColor, txtColor){
			var div = document.createElement('div');
			var lastDiv = this.divs.getLast();
			div.innerHTML = txt;
			$(document.body).adopt(div);
			$(div).adopt(this.generateCloseButton());
			if ($defined(this.options.messageClass))
				div.addClass(this.options.messageClass);
			else
				this.setMessageStyles(div);
			div.setOpacity(0);

			if ($defined(backColor))
				div.setStyle("background-color", backColor);
			
			if ($defined(txtColor))
				div.setStyle("color", txtColor);
				
			//position div
			var divWidth = div.getCoordinates().width;
			var windowWidth = window.getWidth();
			div.setStyle("left", (windowWidth - divWidth)/2);	

			var lastDivBottom = 0;
			if (lastDiv)
			{
				lastDivBottom = lastDiv.getStyle("bottom");
				lastDivBottom = Number.toInt(lastDivBottom);
			};			
			var lastDivHeight = 0;
			if (lastDiv)
				lastDivHeight = lastDiv.getCoordinates().height;
			div.setStyle("bottom", lastDivBottom+lastDivHeight);
			
			var fx = new Fx.Style(div,'opacity',{duration:this.options.appearTime}).start(1);			

			if (this.options.autoClose)
				div.timeOut = this.close.delay(this.options.delayTime, this, div);			
				
			this.divs.push(div);				
		},
		
		close: function(o){
			if ($defined(o.target)) //jesli pochodzi z click eventu
				o=o.target.parentNode;

			if (this.options.autoClose)
				$clear(o.timeOut);

			this.divs.remove(o);
			o.remove();
			this.realign();
		},
		
		realign: function(){
			if (this.divs.length == 0)
			 return;
			 
			this.divs[0].setStyle("bottom", 0);

			for(var i = 1; i < this.divs.length; i++){
				var prevBottom = this.divs[i-1].getStyle("bottom");
				prevBottom = Number.toInt(prevBottom);			
				var prevHeight = this.divs[i-1].getCoordinates().height;			
				this.divs[i].setStyle("bottom", prevBottom+prevHeight);
			}
		}
		
});

Moossage.implement(new Events);
Moossage.implement(new Options);